package com.igeek.boot.tsak;

import org.springframework.cache.annotation.EnableCaching;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @package com.igeek.boot.tsak
 * @Description schedule线程定时任务
 * @Author LittlePunk [296526219@qq.ccom]
 * @Version 2022.1
 * @Date 2023/12/21 10:16
 */
@Component
//开启定时任务
@EnableScheduling
//开启异步任务
@EnableAsync
public class MyTask {

    //单线程会阻塞下一个线程
    @Async
    @Scheduled(cron = "0/5 * * * * ?")
    public void printBy5() throws InterruptedException {
        System.out.println(LocalDateTime.now()+"--MyTask printBy5 正在打印"+Thread.currentThread().getName());
        Thread.sleep(20000);
    }

    @Async
    @Scheduled(cron = "0/10 * * * * ?")
    public void printBy10() throws InterruptedException {

        System.out.println(LocalDateTime.now()+"--MyTask printBy10 正在打印"+Thread.currentThread().getName());
        Thread.sleep(30000);
    }
}
